package com.databing.demo.ama.util

import android.Manifest
import android.annotation.SuppressLint
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.text.TextUtils
import android.util.Log
import androidx.core.app.ActivityCompat
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.databing.demo.DataApp
import com.databing.demo.config.TAG

/**
 * 高德地图的定位
 */

object AMapLocation : AMapLocationListener {

    // 经度
    private var longitude: String = "114.52"

    // 纬度
    private var latitude: String = "38.05"

    //城市
    private var city = "石家庄市"

    //地区
    private var district = "长安区"

    //地址
    private var address: String = ""

    @SuppressLint("StaticFieldLeak")
    private var locationClient: AMapLocationClient? = null


    fun startLocation() {
        if (hasNoPermission() || locationClient != null) return
        //声明mLocationOption对象
        locationClient = AMapLocationClient(DataApp.instance)
        //始化定位参数
        val locationOption = AMapLocationClientOption()
        //设置定位监听
        locationClient!!.setLocationListener(this)
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        //设置定位间隔,单位毫秒,默认为2000ms
        locationOption.interval = 2000
        // locationOption.isOnceLocation = true
        //设置定位参数
        locationClient!!.setLocationOption(locationOption)
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        locationClient!!.startLocation()
    }

    private fun hasNoPermission(): Boolean {
        if (DataApp.instance == null) return true
        return !(PERMISSION_GRANTED == DataApp.instance!!.packageManager.checkPermission(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            "com.databing.demo"
        )
                && PERMISSION_GRANTED == DataApp.instance!!.packageManager.checkPermission(
            Manifest.permission.ACCESS_FINE_LOCATION,
            "com.databing.demo"
        )
                && PERMISSION_GRANTED == DataApp.instance!!.packageManager.checkPermission(
            Manifest.permission.READ_PHONE_STATE,
            "com.databing.demo"
        ))
    }


    private fun stopLocation() {
        if (locationClient != null) {
            locationClient!!.stopLocation()
            locationClient!!.onDestroy()
            locationClient!!.unRegisterLocationListener(this)
            locationClient = null
        }
    }


    override fun onLocationChanged(aMapLocation: AMapLocation?) {
        stopLocation()
        val amLon: AMapLocation = aMapLocation ?: return
        if (amLon.errorCode == 0) {
            longitude = amLon.longitude.toString()
            latitude = amLon.latitude.toString()
            city = amLon.city
            district = amLon.district
            address = amLon.address
            if (!TextUtils.isEmpty(amLon.street) && !TextUtils.isEmpty(amLon.streetNum)) {
                val streetNum =
                    if (amLon.streetNum.endsWith("号")) "${amLon.street}${amLon.streetNum}" else "${amLon.street}${amLon.streetNum}号"
                Log.e(TAG, "$latitude : $longitude : $city : $district : $address ：$streetNum")
            } else if (!TextUtils.isEmpty(amLon.street) && !TextUtils.isEmpty(amLon.streetNum)) {
                val streetNum = "$city$district"
                Log.e(TAG, "$latitude : $longitude : $city : $district : $address ：$streetNum")

            }
        }
    }

}